//
//  JDCityShareViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/2/6.
//

import UIKit
/// 废弃状态
class JDCityShareViewController: JDBaseViewController {

//    var classView: DraClassicHeadView?
//    var classTitleList: [String]?
    
    var mainTableView: UITableView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.removeSeparatorLine()
        self.setupConfig()
        self.setupUIElements()
        // MARK: 这里需要一个定位
        self.getNetData()
        // Do any additional setup after loading the view.
    }
    
    func setupConfig() {
        self.title = "同城分享"
        self.removeSeparatorLine()
        self.customLeftBarButtonItem("xlbLogo", #selector(leftButtonAction), self)
    }

    @objc func leftButtonAction() {
        print("左上角的按钮")
    }
    
    func setupUIElements() {
//        self.classView = DraClassicHeadView.init(CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width - 85, height: 40), titles: self.classTitleList!, index: 0)
//        self.view.addSubview(self.classView!)
//        self.classView?.snp.makeConstraints({ make in
//            make.left.equalTo(self.view)
//            make.right.equalTo(self.view)//.offset(-85)
//            make.top.equalTo(self.view).offset(100)
//            make.height.equalTo(40)
//        })
//        self.classView?.backgroundColor = .orange
//
//        DispatchQueue.main.asyncAfter(deadline: .now()+5) {
//            self.classView?.updateData(["aaa","bbb"], index: 0)
//        }
        
        
        self.mainTableView = UITableView(frame: .zero, style: .plain)
        self.view.addSubview(self.mainTableView!)
        self.mainTableView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
//            make.top.equalTo(self.classView!.snp.bottom)
            make.top.equalTo(self.view).offset(UIDevice().safeAreaInset.top)
            make.bottom.equalTo(self.view)
        })
        self.mainTableView?.delegate = self
        self.mainTableView?.dataSource = self
        self.mainTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")
        self.mainTableView?.separatorStyle = .none
        self.mainTableView?.backgroundColor = .red
    }
    
    func getNetData() {
        printLog("获取接口数据，刷新列表信息")
        var params = [String: Any]()
        params["onlineSign"] = JDLoginModel.default.onlineSign
        params["pageNo"] = self.currentPage
        params["pageSize"] = 10
        params["lon"] = "121.6" //经度
        params["lat"] = "38.9" //纬度
        JDNetWorkRequest(.cityShareList(params)) { responseStr in
            
            print("列表请求成功\(responseStr)")
//            {"returnCode":"00","returnMsg":null,"body":{"totalrecord":0,"list":[]}}
        } failure: { error in
            print("列表请求失败")
        } error: { errorCode in
            print("列表请求异常\(errorCode)")
        }

        
        
        
        
        
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

    @objc func addDragonUnionAction(_ currentButton: UIButton) {
        let currentTag = currentButton.tag - 1234
        printLog("加入龙商圈\(currentTag)")
        
        var params = [String: Any]()
        params["onlineSign"] = JDLoginModel.default.onlineSign
        params["userid"] = JDLoginModel.default.userid
        // MARK: 这个要改成对方的id
        params["ruserid"] = JDLoginModel.default.userid
        
        JDNetWorkRequest(.addDragonUnion(params)) { responseStr in
            print("邀请成功\(responseStr)")
//            邀请成功{"returnCode":"00","returnMsg":null,"body":null}
        } failure: { error in
            print("邀请失败")
        } error: { errorCode in
            print("邀请错误\(errorCode)")
        }

        
    }
    
}

extension JDCityShareViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell")
        
        for subV in cell?.contentView.subviews ?? [UIView]() {
            subV.removeFromSuperview()
        }
        cell?.contentView.backgroundColor = color("#F5F6F8")

        let containerView = UIView()
        cell?.contentView.addSubview(containerView)
        containerView.snp.makeConstraints { make in
            make.left.equalTo(cell!.contentView).offset(12)
            make.right.equalTo(cell!.contentView).offset(-12)
            make.top.equalTo(cell!.contentView)
            make.bottom.equalTo(cell!.contentView).offset(-10)
        }
        containerView.layer.cornerRadius = 8
        containerView.backgroundColor = .white
        
        
        let shareImgV = UIImageView()
        containerView.addSubview(shareImgV)
        shareImgV.snp.makeConstraints { make in
            make.left.equalTo(containerView).offset(12)
            make.top.equalTo(containerView).offset(15)
            make.size.equalTo(CGSize(width: 74, height: 74))
        }
        shareImgV.backgroundColor = .green
        
        let goodsNameLabel = UILabel()
        containerView.addSubview(goodsNameLabel)
        goodsNameLabel.snp.makeConstraints { make in
            make.left.equalTo(shareImgV.snp.right).offset(15)
            make.top.equalTo(shareImgV)
            make.right.equalTo(containerView).offset(-12)
        }
        goodsNameLabel.text = "泰国龙眼新鲜进口5斤鲜桂圆非国产泰国龙眼新鲜进口5斤鲜桂圆非国产"
        goodsNameLabel.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        goodsNameLabel.textColor = color("#333333")
        goodsNameLabel.numberOfLines = 2
        
        let createShareTime = UILabel()
        containerView.addSubview(createShareTime)
        createShareTime.snp.makeConstraints { make in
            make.left.equalTo(shareImgV.snp.right).offset(15)
            make.bottom.equalTo(shareImgV).offset(-7)
        }
        createShareTime.font = UIFont.systemFont(ofSize: 12, weight: .regular)
        createShareTime.textColor = color("#333333")
        createShareTime.text = "2024/10/10"
        
        let bottomLine = UIView()
        containerView.addSubview(bottomLine)
        bottomLine.snp.makeConstraints { make in
            make.left.equalTo(containerView).offset(12)
            make.height.equalTo(2)
            make.top.equalTo(shareImgV.snp.bottom).offset(16)
            make.right.equalTo(containerView).offset(-12)
            make.bottom.equalTo(containerView).offset(-50)
        }
        bottomLine.backgroundColor = color("#F5F6F8")
        
        let storeNameLabel = UILabel()
        containerView.addSubview(storeNameLabel)
        storeNameLabel.snp.makeConstraints { make in
            make.left.equalTo(containerView).offset(12)
            make.top.equalTo(bottomLine.snp.bottom)
            make.bottom.equalTo(containerView)
        }
        storeNameLabel.text = "百香园水果超市"
        storeNameLabel.textColor = color("#333333")
        storeNameLabel.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        
        let addDragonUnion = UIButton(type: .custom)
        containerView.addSubview(addDragonUnion)
        addDragonUnion.snp.makeConstraints { make in
            make.right.equalTo(containerView).offset(-12)
            make.top.equalTo(bottomLine.snp.bottom)
            make.bottom.equalTo(containerView)
        }
        addDragonUnion.setTitle("邀加龙商圈", for: .normal)
        addDragonUnion.tag = indexPath.row + 1234
        addDragonUnion.addTarget(self, action: #selector(addDragonUnionAction(_:)), for: .touchUpInside)
        addDragonUnion.setTitleColor(color("#333333"), for: .normal)
        addDragonUnion.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .regular)
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
}
